package controller;

import dao.UserDao;
import pojo.user.UserEntity;
import utils.JudgeUtil;

import java.io.IOException;

/**
 * @author Limohan
 */
public class UserController {
    private final UserDao userDao = new UserDao();
    private static final UserController userController = new UserController();
    private UserController(){}

    public static UserController getInstance(){
        return userController;
    }

    //注册
    public String register(UserEntity userEntity) throws IOException {
        JudgeUtil judgeUtil = new JudgeUtil(userEntity);

        userEntity.setId("1");
        String id = userDao.getMaxId();
        userEntity.setId(id);
        if(judgeUtil.judgeInput()==null){
            userDao.saveUser(userEntity);
            return "注册成功";
        }
        return judgeUtil.judgeInput();
    }

    //登陆
    public String logIn(String account, String password) throws IOException {
        if(account.equals("admin"))
            return "admin";
        //找到对应的UserEntity
        UserEntity userEntity=userDao.findUser(account);
        if(userEntity==null){
            return "该用户不存在";
        }else{
            if(userEntity.getPassword().equals(password)){
                    return userEntity.getPower();
            }else{
                return  "密码不正确";
            }
        }
    }
}

